#!/bin/bash

if [ "$#" -eq "0" ] || [ "$1" = "-h" ]
then
	echo -e "Usage:\n\t$0 basic_file_name [advance_file_name] [high_file_name]"
	exit -1
fi
need_to_parse_file=$1
current_time=`date "+%y_%m_%d_%H_%M_%S"`
exec 1> TestReport_Parsed_${current_time}.csv

echo "Entire Frame,,,,,,,Left Eye,,,,,,,Right Eye,,,,,,,Latency (ms),,,KHR_mutable_render_buffer,EGL_ANDROID_front_buffer_auto_refresh
Submit FPS (Hz),,,Submit Frame Time (ms),,,Missed Rate (%),Timewarp FPS (Hz),,,Timewarp Frame Time (ms),,,Missed Rate (%),Timewarp FPS (Hz),,,Timewarp Frame Time (ms),,,Missed Rate (%),,,,,
Max,Min,Mean,Max,Min,Mean,,Max,Min,Mean,Max,Min,Mean,,Max,Min,Mean,Max,Min,Mean,,Max,Min,Mean
"
while [ -n "$1" ]
do
	gawk 'BEGIN{FS=",";arr_index = 0;arr_egl_index = 0;} 
	{
		if((NR >= 13 && NR < 17 ) || NR >= 23)
		{
			if($1 == "Max")
			{
				arr_criteria[arr_index] = $3;
				arr_value[arr_index] = $2;
				arr_result[arr_index] = $5;
				arr_index++;
			}
	
			if($1 == "Min")
			{
				arr_criteria[arr_index] = $3;
				arr_value[arr_index] = $2;
				arr_result[arr_index] = $5;
				arr_index++;
			}
	
			if($1 == "Mean")
			{
				arr_criteria[arr_index] = $3;
				arr_value[arr_index] = $2;
				arr_result[arr_index] = $5;
				arr_index++;
			}
	
			if($1 == "Missed Rate")
			{
				arr_criteria[arr_index] = $3;
				arr_value[arr_index] = $2;
				arr_result[arr_index] = $5;
				arr_index++;
			}
	
			if($1 == "KHR_mutable_render_buffer")
			{
				arr_egl_ex_criterial[arr_egl_index] = $3;
				arr_egl_ex_value[arr_egl_index] = $2;
				arr_egl_ex_result[arr_egl_index] = $5;
				arr_egl_index++;
			}
	
			if($1 == "EGL_ANDROID_front_buffer_auto_refresh")
			{
				arr_egl_ex_criterial[arr_egl_index] = $3;
				arr_egl_ex_value[arr_egl_index] = $2;
				arr_egl_ex_result[arr_egl_index] = $5;
				arr_egl_index++;
			}
		}
	} END {
		//output criteria
		for( loop_i =0; loop_i < arr_index; loop_i++)
		{
			if(loop_i == 10)
			{
				for(loop_j = loop_i + 3; loop_j <=16; loop_j++)
				{
					printf "%.6f,",arr_criteria[loop_j];
				}

				for(loop_k = loop_i; loop_k <= 12; loop_k++)
				{
					printf "%.6f,",arr_criteria[loop_k];
				}
				loop_i += 7;
			}
			printf "%.6f,",arr_criteria[loop_i];
		}
		for( loop_i =0; loop_i < arr_egl_index; loop_i++)
		{
			printf "%s,",arr_egl_ex_criterial[loop_i];
		}
		print "\r";
	
		//output value
		for( loop_i =0; loop_i < arr_index; loop_i++)
		{
			if(loop_i == 10)
			{
				for(loop_j = loop_i + 3; loop_j <=16; loop_j++)
				{
					printf "%.6f,",arr_value[loop_j];
				}

				for(loop_k = loop_i; loop_k <= 12; loop_k++)
				{
					printf "%.6f,",arr_value[loop_k];
				}
				loop_i += 7;
			}
			printf "%.6f,",arr_value[loop_i];
		}
		for( loop_i =0; loop_i < arr_egl_index; loop_i++)
		{
			printf "%s,",arr_egl_ex_value[loop_i];
		}
	
		print "\r";
		//output result
		for( loop_i =0; loop_i < arr_index; loop_i++)
		{
			if(loop_i == 10)
			{
				for(loop_j = loop_i + 3; loop_j <=16; loop_j++)
				{
					printf "%s,",arr_result[loop_j];
				}

				for(loop_k = loop_i; loop_k <= 12; loop_k++)
				{
					printf "%s,",arr_result[loop_k];
				}
				loop_i += 7;
			}
			printf "%s,",arr_result[loop_i];
		}
		for( loop_i =0; loop_i < arr_egl_index; loop_i++)
		{
			printf "%s,",arr_egl_ex_result[loop_i];
		}
		print "\r";
	}' $1
	shift
done
